//
//  HMComposeTextView.swift
//  CZWeibo31
//
//  Created by apple on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMComposeTextView: UITextView {
    
    // 重写textView font属性
    override var font: UIFont?{
        didSet{
            // 设置占位文字的font
            placeholderLabel.font = font
        }
    }
    // 显示的占位文字
    var placeholder: String?{
        didSet{
            // 设置占位文字的text
            placeholderLabel.text = placeholder
        }
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
//        let txt = UITextField()
////        self.placeholder
        // 添加控件
        addSubview(placeholderLabel)
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(SCREENW - 10)
        }
        // 监听系统通知 查看textView 文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    // 监听文字改变
    func textViewTextDidChange(){
        placeholderLabel.hidden = self.hasText()
    }
    
    // MARK: - 懒加载控件
    private lazy var placeholderLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.grayColor()
        lab.numberOfLines = 0
        return lab
    }()
    // 析构函数
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
